#include "physics/pship.h"

#include <QDomDocument>

PShip::PShip(PWorld *world, QString name) : PObject(world, QString("ship_") + name)
{
    setMaxEnergy(100);
    myAlpha = 0;
    myDAlpha = 0.18;
    setM(0.001);

    myEnergy = 100;
    myEngine = 0.03;
    myBulletSpeed = 2.;
    myEnergyForBullet = 10;
    myEnergyForEngine = 2;
    setDamage(30);
    acceleration = false;
    right = false;
    left = false;
    shoot = false;
}

void PShip::incEnergy(double energy) {
    myEnergy += energy;
    if (myEnergy > myMaxEnergy) {
        myEnergy = myMaxEnergy;
    }
}

void PShip::decEnergy(double energy) {
    myEnergy -= energy;
    if (myEnergy < 0) {
        myEnergy = 0;
    }
}

bool PShip::isAccelerating() const {
    return acceleration;
}

bool PShip::isRight() const {
    return right;
}

bool PShip::isLeft() const {
    return left;
}

bool PShip::isFire() const {
    return shoot;
}

void PShip::setAccelerate(bool b) {
    acceleration = b;
}

void PShip::setRight(bool b) {
    right = b;
}

void PShip::setLeft(bool b) {
    left = b;
}

void PShip::setFire(bool b) {
    shoot = b;
}

void PShip::setAlpha(double alpha) {
    myAlpha = alpha;
}

double PShip::getAlpha() const {
    return myAlpha;
}

void PShip::turnLeft() {
    myAlpha += myDAlpha;
}

void PShip::turnRight() {
    myAlpha -= myDAlpha;
}

double PShip::getEngine() const {
    return myEngine;
}

double PShip::getBulletSpeed() const {
    return myBulletSpeed;
}

double PShip::getEnergyForBullet() const {
    return myEnergyForBullet;
}

void PShip::setMaxEnergy(double maxEnergy) {
    myMaxEnergy = maxEnergy;
}

double PShip::getMaxEnergy() const {
    return myMaxEnergy;
}

void PShip::setEnergy(double energy) {
    myEnergy = energy;
}

double PShip::getEnergy() const {
    return myEnergy;
}

double PShip::getEnergyForEngine() const {
    return myEnergyForEngine;
}

void PShip::serialize(QDomDocument *doc, QDomElement *e) const {
    e->setAttribute("type", "Ship");
    PObject::serialize(doc, e);

    QDomNode alpha = doc->createElement("Alpha");
    QDomNode alpha_v = doc->createTextNode(QString("%1").arg(myAlpha));
    alpha.appendChild(alpha_v);
    e->appendChild(alpha);

    QDomNode dalpha = doc->createElement("DAlpha");
    QDomNode dalpha_v = doc->createTextNode(QString("%1").arg(myDAlpha));
    dalpha.appendChild(dalpha_v);
    e->appendChild(dalpha);

    QDomNode engine = doc->createElement("Engine");
    QDomNode engine_v = doc->createTextNode(QString("%1").arg(myEngine));
    engine.appendChild(engine_v);
    e->appendChild(engine);

    QDomNode bulletSpeed = doc->createElement("BulletSpeed");
    QDomNode bulletSpeed_v = doc->createTextNode(QString("%1").arg(myBulletSpeed));
    bulletSpeed.appendChild(bulletSpeed_v);
    e->appendChild(bulletSpeed);

    QDomNode energyForBullet = doc->createElement("EnergyForBullet");
    QDomNode energyForBullet_v = doc->createTextNode(QString("%1").arg(myEnergyForBullet));
    energyForBullet.appendChild(energyForBullet_v);
    e->appendChild(energyForBullet);

    QDomNode energyForEngine = doc->createElement("EnergyForEngine");
    QDomNode energyForEngine_v = doc->createTextNode(QString("%1").arg(myEnergyForEngine));
    energyForEngine.appendChild(energyForEngine_v);
    e->appendChild(energyForEngine);

    QDomNode energy = doc->createElement("Energy");
    QDomNode energy_v = doc->createTextNode(QString("%1").arg(myEnergy));
    energy.appendChild(energy_v);
    e->appendChild(energy);

    QDomNode maxEnergy = doc->createElement("MaxEnergy");
    QDomNode maxEnergy_v = doc->createTextNode(QString("%1").arg(myMaxEnergy));
    maxEnergy.appendChild(maxEnergy_v);
    e->appendChild(maxEnergy);
}

PShip *PShip::deserialize(QDomNode *e, PWorld *parent) {
    PObject *obj = PObject::deserialize(e, 0);
    PShip *ship = new PShip(parent, obj->getName());

    ship->setBrightness(obj->getBrightness());
    ship->setDamage(obj->getDamage());
    ship->setLife(obj->getLife());
    ship->setM(obj->getM());
    ship->setPos(obj->getPos());
    ship->setR(obj->getR());
    ship->setV(obj->getV());

    QDomNode n = e->firstChild();
    while (!n.isNull()) {
        QString name = n.toElement().tagName();
        if (name == "Alpha") {
            ship->myAlpha = n.firstChild().toText().data().toDouble();
        } else if (name == "DAlpha") {
            ship->myDAlpha = n.firstChild().toText().data().toDouble();
        } else if (name == "Engine") {
            ship->myEngine = n.firstChild().toText().data().toDouble();
        } else if (name == "BulletSpeed") {
            ship->myBulletSpeed = n.firstChild().toText().data().toDouble();
        } else if (name == "EnergyForBullet") {
            ship->myEnergyForBullet = n.firstChild().toText().data().toDouble();
        } else if (name == "EnergyForEngine") {
            ship->myEnergyForEngine = n.firstChild().toText().data().toDouble();
        } else if (name == "Energy") {
            ship->myEnergy = n.firstChild().toText().data().toDouble();
        } else if (name == "MaxEnergy") {
            ship->myMaxEnergy = n.firstChild().toText().data().toDouble();
        }
        n = n.nextSibling();
    }

    delete obj;
    return ship;
}
